<template>
	<view class="margin-top bg-white padding-lr">
		<u-form :model="info" ref="form" label-width="auto">
			<u-form-item label="规则名称"><text class="text-bold padding-left">{{info.ruleName}}</text></u-form-item>
			<u-form-item label="规则数据" prop="ruleData" required><u-input placeholder='{"a":2,"b":3}(请输入JSON对象字符串)' type="textarea" v-model="info.ruleData" height="800" :maxlength="-1" :border="true" /></u-form-item>
			<u-form-item label="测试结果"><text class="text-bold padding-left">{{JSON.stringify(result)}}</text></u-form-item>
			<view class="padding-tb"><u-button type="primary" @click="execute">执行</u-button></view>
		</u-form>
	</view>
</template>

<script>
export default {
	onLoad: function(option) {
		if (option.info) {
			this.info = JSON.parse(decodeURIComponent(option.info));
		}
	},
	onReady: function() {
		this.$refs.form.setRules(this.rules);
	},
	data() {
		return {
			show: false, // 显示选项
			info: {
				ruleCode: '',
				ruleName: '',
				ruleData: ''
			},
			test: {
				ruleCode: '',
				ruleData: {}
			},
			rules: {
				ruleData: [{ required: true, message: '必填', trigger: ['change', 'blur'] }]
			},
			result: {}
		};
	},
	methods: {
		execute: function() {
			this.$refs['form'].validate(valid => {
				if (valid) {
					this.test.ruleCode = this.info.ruleCode;
					try {
						this.test.ruleData = JSON.parse(this.info.ruleData);
					} catch (e) {
						this.$utils.showToast("数据字符串格式不正确！");
						return;
					}
					this.$request.post(
						'/admin/sys/drools/drl/execute',
						this.test,
						res => {
							this.result = (res.data || {}).ruleData || {};
						},
						false,
						true,
						true
					);
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped></style>
